<?php
namespace PcHome\Controller;
use Think\Upload;
use Common\Lib\Think\Page;
use Common\Lib\Util\Tree;
use Think\Storage\Driver\File;
use Common\Controller\AdminBaseController;

class AdminArticleController extends AdminBaseController
{
    const CNAME = '文章';

    protected $article_model;
    protected $articleCate_model;

    public function _initialize() {
        parent::_initialize();

        $this->article_model = D("Article");
        $this->articleCate_model = D("ArticleCate");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 文章列表
     */
    public function index() {
        $keyword = I('post.keyword', '', 'htmlspecialchars');
        $where = array('is_delete' => 0);
        if ($keyword) {
            $where['title'] = array('LIKE',  '%' . $keyword . '%');
            $this->assign('keyword', $keyword);
        }

        // 分页
        $count = $this->article_model->where($where)->count();
        $page = new Page($count, C('PAGE_LIST_ROWS'));
        $show = $page->show();
        $list = $this->article_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $cates = $this->articleCate_model->fetchAll();
        $this->assign('cates', $cates);
        $this->display(); // 输出模板
    }

    /**
     * 回收站列表
     */
    public function recycleList() {
        $keyword = I('post.keyword', '', 'htmlspecialchars');
        $where = array('is_delete' => 1);
        if ($keyword) {
            $where['title'] = array('LIKE',  '%' . $keyword . '%');
            $this->assign('keyword', $keyword);
        }

        // 分页
        $count = $this->article_model->where($where)->count();
        $page = new Page($count, C('PAGE_LIST_ROWS'));
        $show = $page->show();
        $list = $this->article_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display(); // 输出模板
    }

    /**
     * 文章添加
     */
    public function add() {
        if (IS_POST) {
            $data = $this->article_model->create();
            if ($data) {
                $id = $this->article_model->add($data);
                if ($id) {
                    $tag_ids = I('tag_ids');
                    if (is_array($tag_ids)) {
                        $articleTag = D('ArticleTag');
                        foreach ($tag_ids as $val) {
                            if (empty($val)) {
                                continue;
                            }
                            $data = array(
                                'article_id' => $id,
                                'tag_id'     => intval($val)
                            );
                            $articleTag->add($data);
                        }
                    }
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->article_model->getError());
            }

        } else {
            $tree = new Tree();
            $tree->tree($this->articleCate_model->fetchAll(),'cate_id','parent_id','cate_name');
            $cates = $tree->getArray();
            $this->assign('cates', $cates);
            $this->assign('tags', D('Tag')->fetchAll());
            $this->display();
        }

    }


    public function edit() {

        $article_id = I('article_id', 0, 'intval');

        if (empty($article_id) || !$detail = $this->article_model->find($article_id)) {
            $this->error('请选择要编辑的' . self::CNAME);
        }

        if (IS_POST) {
            $data = $this->article_model->create();
            if ($data) {
                if ($this->article_model->save($data) !== false) {
                    $tag_ids = I('tag_ids');
                    if (is_array($tag_ids)) {
                        $articleTag = D('ArticleTag');
                        $articleTag->where(array('article_id' => $article_id))->delete();
                        foreach ($tag_ids as $val) {
                            if (empty($val)) {
                                continue;
                            }
                            $data = array(
                                'article_id' => $article_id,
                                'tag_id'      => intval($val)
                            );
                            $articleTag->add($data);
                        }
                    }
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->ad_model->getError());
            }

        } else {
            $tree = new Tree();
            $tree->tree($this->articleCate_model->fetchAll(),'cate_id','parent_id','cate_name');
            $this->assign('cates', $tree->getArray());

            $this->assign('detail', $detail);
            $this->assign('tags', D('Tag')->fetchAll());

            $this->assign('articletags', implode(',', D('ArticleTag')->getTagsByArticleId($article_id)));

            $this->display();
        }
    }

    /*
     * 删除到回收站
     */
    public function delete() {

        $article_id = I('post.article_id', 0, 'intval');

        if (is_numeric($article_id)) {
            $this->article_model->save(array('article_id' => $article_id, 'is_delete' => 1));
            $this->success('成功删除到回收站', U('index'));
        } elseif (is_array($article_id)) {
            foreach ($article_id as $id) {
                $this->article_model->save(array('article_id' => $id, 'is_delete' => 1));
            }
            $this->success('成功批量删除到回收站', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /*
     * 改变文章发布状态
     */
    public function changePublish() {
        $article_id = I('post.article_id', 0, 'intval');

        if (!empty($article_id)) {

            if (!$detail = $this->article_model->find($article_id)) {
                $this->error('请选择要改变状态的' . self::CNAME);
            }
            $data['article_id']  = $article_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->article_model->save($data) !== false) {
                $this->success(self::CNAME . '状态改变成功', U('index'));
            } else {
                $this->error(self::CNAME . '状态改变失败');
            }
        } else {
            $this->error('请选择要改变状态的' . self::CNAME);
        }
    }

    /*
     * 恢复回收站文章
     */
    public function recovery() {
        $article_id = I('post.article_id', 0, 'intval');

        if ($this->article_model->save(
            array(
            'article_id' => $article_id,
            'is_delete' => 0
            )) !== false){
            $this->success(self::CNAME . '恢复成功', U('recoveryList'));
        } else {
            $this->error(self::CNAME . '恢复失败');
        }
    }

    /*
     * 彻底删除文章
     */
    public function deleteCompletely() {

        $article_id = I('post.article_id', 0, 'intval');

        if (is_numeric($article_id)) {
            $this->article_model->delete($article_id);
            $this->success(self::CNAME . '彻底删除成功', U('index'));
        } elseif (is_array($article_id)) {
            $this->article_model->delete(implode(',', $article_id));
            $this->success(self::CNAME . '批量彻底删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    public function delimg(){

    }
}